<?php
// composer下载方式
// 先使用composer命令下载：
// composer require owner888/phpspider
// 引入加载器
//require './vendor/autoload.php';

// GitHub下载方式
require_once __DIR__ . '/../autoloader.php';
use phpspider\core\phpspider;
use phpspider\core\requests;
use phpspider\core\selector;
use phpspider\core\db;



/**************从这里开始运行前测试**********************/
$url = "http://goucai.diyicai.com/lottery/jchtplayvsForJsp.action?lotteryId=47&type=jcmini";
$html = requests::get($url);

//采集足球队1 数据
$selectorForTerm1 = "//td[contains(@class,'wh-4')]//a";
$term1 = selector::select($html, $selectorForTerm1);

//采集足球队2 数据
$selectorForTerm2 = "//td[contains(@class,'wh-6')]//a";
$term2 = selector::select($html, $selectorForTerm2);

//采集term1获胜中奖率数据
$selectorForWin = "//td[contains(@class,'wh-8')]/div[contains(@class,'frq')]/a[1]";
$win = selector::select($html, $selectorForWin);

//采集平局中奖率数据
$selectorForDogfall = "//td[contains(@class,'wh-8')]/div[contains(@class,'frq')]/a[2]";
$dogfall = selector::select($html, $selectorForDogfall);

//采集term2获胜率数据
$selectorForLose = "//td[contains(@class,'wh-8')]/div[contains(@class,'frq')]/a[3]";
$lose = selector::select($html, $selectorForLose);
// var_dump($win);die;


$data = array(
    'term1' => $term1,
    'term2' => $term2,
    'win' => $win,
    'dogfall' => $dogfall,
    'lose' => $lose,
);

foreach ($data as $key => $value) {
    switch ($key) {
        case 'term1':
            foreach ($value as $k => $v) {
                $newData[$k][$key] = $v;
            }
            break;

        case 'term2':
            foreach ($value as $k => $v) {
                $newData[$k][$key] = $v;
            }
            break;

        case 'win':
            foreach ($value as $k => $v) {
                $newData[$k][$key] = $v;
            }
            break;

        case 'dogfall':
            foreach ($value as $k => $v) {
                $newData[$k][$key] = $v;
            }
            break;

        case 'lose':
            foreach ($value as $k => $v) {
                $newData[$k][$key] = $v;
            }
            break;

    }
}

// var_dump($newData);die;

$db_config = array(
    'host'  => '127.0.0.1',
    'port'  => 3306,
    'user'  => 'root',
    'pass'  => '123456',
    'name'  => 'phpspider',
);
// 数据库配置
db::set_connect('default', $db_config);
// 数据库链接
db::init_mysql();

foreach ($newData as $data) {

    $result = db::insert("caipiao", $data);

}

if ($result) {
    echo 'success';
} else {
    echo 'error';
}
/*********************运行前测试结束*************************/








